package org.atf.utils;

import java.io.IOException;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

public class TAMIntegrationFilter  implements Filter{

	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		HttpServletRequest req = (HttpServletRequest) request;
        HeaderMapRequestWrapper requestWrapper = new HeaderMapRequestWrapper(req);
        String user_header= ATFUtils.getAppUserHeader();
        if(req.getHeader(user_header) == null){
        	if(ATFUtils.isTestMode()){
        		requestWrapper.addHeader(user_header, "TEST");
        		
        	}else{
        		requestWrapper.addHeader(user_header, "TEST");
        	}
        }
        String groupStrHeader= ATFUtils.getAppGroupsHeader();
        if(req.getHeader(groupStrHeader) == null){
        	if(ATFUtils.isTestMode()){
        		requestWrapper.addHeader(groupStrHeader, "ALL");
        	}else{
        		requestWrapper.addHeader(groupStrHeader, "restart_PCA,WebSphereConsoleUser");
        	}
        }
        chain.doFilter(requestWrapper, response); // Goes to default servlet.
	}

	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		
	}
	public class HeaderMapRequestWrapper extends HttpServletRequestWrapper {

		public HeaderMapRequestWrapper(HttpServletRequest request) {
			super(request);
			// TODO Auto-generated constructor stub
		}
		private Map<String, String> headerMap = new HashMap<String, String>();
		public void addHeader(String name, String value) {
            headerMap.put(name, value);
        }
		 @Override
        public String getHeader(String name) {
            String headerValue = super.getHeader(name);
            if (headerMap.containsKey(name)) {
                headerValue = headerMap.get(name);
            }
            return headerValue;
        }
		 @Override
        public Enumeration<String> getHeaderNames() {
            List<String> names = Collections.list(super.getHeaderNames());
            for (String name : headerMap.keySet()) {
                names.add(name);
            }
            return Collections.enumeration(names);
        }
		 @Override
        public Enumeration<String> getHeaders(String name) {
            List<String> values = Collections.list(super.getHeaders(name));
            if (headerMap.containsKey(name)) {
                values.add(headerMap.get(name));
            }
            return Collections.enumeration(values);
        }

	}

}
